//
//  PictureViewController.m
//  GOGOTravel
//
//  Created by dllo on 16/2/27.
//  Copyright © 2016年 果果. All rights reserved.
//

#import "PictureViewController.h"
#import "PICmodel.h"
#import "PICCollectionViewCell.h"
#import "CityViewController.h"
#import "PCViewController.h"
@interface PictureViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain) NSMutableArray *pictureArr;
@property (nonatomic, retain) NSMutableArray *modelArr;
@property (nonatomic, retain) BaseCollectionView *collection;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, retain) NSString *startUrl;
@property (nonatomic, assign) BOOL isHeader;
@property (nonatomic, retain) NSString *urlStr;
@end

@implementation PictureViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.pictureArr = [NSMutableArray array];
        self.modelArr = [NSMutableArray array];
    }
    return self;
}
- (void)dealloc{
    [_pictureArr release];
    [_modelArr release];
    [_collection release];
    [_startUrl release];
    [_urlStr release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = UIColorFromRGB(0xF0F0F0);
    
    
    self.pictureArr = [NSMutableArray array];
    
    self.modelArr = [NSMutableArray array];
    
    
    
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 60)];
    view.backgroundColor = [UIColor colorWithRed:0.9 green:0.29 blue:0.44 alpha:1];
    
    [self.view addSubview:view];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10, 30, 20, 20);
    [button setBackgroundImage:[UIImage imageNamed:@"iconfont-jiantouzuo-2.png"] forState:UIControlStateNormal];
    
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:button];
    
    
    UILabel *label = [[UILabel alloc] init];
    
    label.frame = CGRectMake(0, 25, 200, 50);
    label.text = @"全部图片";
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont boldSystemFontOfSize:18];
    label.textAlignment = NSTextAlignmentCenter;
    label.center = view.center;
    [view addSubview:label];
    
    [label release];
    
    
     _page = 0;
    self.startUrl = @"";
    [self handleData];
    
    [self createCollection];
    [self addHeaderRef];
    [self addFooterRef];

}

- (void)buttonAction:(UIButton *)button{
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
    
}

- (void)addHeaderRef{
    
    [_collection addHeaderWithCallback:^{
        _startUrl = @"";
        _page = 0;
        _isHeader = YES;
        [self handleData];
        
    }];
    
    
}

- (void)addFooterRef{
    
    [_collection addFooterWithCallback:^{
       
        _page = _page + 18;
        
        
        _isHeader = NO;
        
        _startUrl = [NSString stringWithFormat:@"&start=%ld", (long)_page];
        
        

        
        
        [self handleData];
        
    }];
    
    
}




- (void)createCollection{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((self.view.frame.size.width - 40)/3, (self.view.frame.size.width - 40)/2-20);
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);

    self.collection = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 60, self.view.frame.size.width, self.view.frame.size.height-60) collectionViewLayout:layout];
    
    _collection.delegate = self;
    _collection.dataSource = self;
   
    [self.view addSubview:_collection];
    [_collection release];
    
    [_collection registerClass:[PICCollectionViewCell class] forCellWithReuseIdentifier:@"PICCollectionViewCell"];
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    PICCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PICCollectionViewCell" forIndexPath:indexPath];
    NSString *str = _pictureArr[indexPath.row];
    
    [cell.image1 sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:PLACE_HLODER]];
    cell.layer.cornerRadius = 5;
    cell.layer.masksToBounds = YES;
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    PCViewController *pc = [[PCViewController alloc] init];
    
    pc.dataModelArr = _modelArr;
    
    NSString *image = _pictureArr[indexPath.row];
    
    pc.picFirst = image;
    pc.row = indexPath.row;
    pc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:pc animated:YES completion:^{
        
    }];
    
    [pc release];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _pictureArr.count;
}
- (void)handleData{
    
    NSString *str = @"zhang/photos/?gallery_mode=1&count=18&guoguo&sign=5a43d03ddffd34758e0d5bb6873a3065";
    
    NSString *str1 = [str stringByReplacingOccurrencesOfString:@"&guoguo" withString:_startUrl];
    
    NSString *resultUrl = [str1 stringByReplacingOccurrencesOfString:@"zhang/" withString:_urlFirst];
    
    
    
    
   
    [AFNetWorkingHandle GETRequestWithURLString:resultUrl completionWithSuccess:^(id result) {
     
        
        NSDictionary *dic = result;
        NSArray *items = dic[@"items"];
        
        if (_isHeader == YES) {
            [_pictureArr removeAllObjects];
            [_modelArr removeAllObjects];
        }
        
        
        NSMutableArray *arr = [NSMutableArray array];
        
        //将全部图片加到数组中
        for (NSDictionary *dic in items) {
            NSString *image = dic[@"photo"];
            [arr addObject:image];
        }
        
        for (NSString *image in arr) {
            [_pictureArr addObject:image];
        }
        
        NSArray *arrModel = [NSArray array];
        
        
        arrModel = [PICmodel ModelHandleWithArray:items];
        
        for (PICmodel *model in arrModel) {
            [_modelArr addObject:model];
        }
        
        
        
        [_collection reloadData];
        
        [_collection headerEndRefreshing];
        [_collection footerEndRefreshing];
    } withFailure:^(id error) {
        
    }];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
